package com.allenlee.mvphelper.net.retrofit.exception;

import android.text.TextUtils;

import com.allenlee.mvphelper.module.login.LoginContract;
import com.allenlee.mvphelper.util.encryption.Base64Helper;
import com.allenlee.mvphelper.util.encryption.MD5Helper;

import java.util.HashMap;

/**
 * @author : lifei
 * 创建时间： 19-11-29
 * 更改时间： 19-11-29
 * Email : allen.lee723@qq.com
 * @version :
 * Description：
 */
public class APIException extends RuntimeException {

    /*
    1.登录接口
        200: 登录成功
        201: imei为空
        202: 用户名为空
        203: 密码为空
        204: 客户端无效
        205: 账号或密码错误

    2.授权请求接口 -- 在界面提示用户
        300: 授权申请成功
        301: 用户名为空
        302： sn码为空
        303: 用户名或sn码错误
        304: 授权时间过期
        305: 单次授权次数超额
        306: 总授权次数超额
        307: 流水号错误

    3.授权返回接口 -- 不在界面提示用户，后台操作
        500: 授权成功
        501: 用户名为空
        502: sn码为空
        503: 设备已授权
        504: 授权失败
        503: 用户名或sn码错误
    */

    // 1.登录接口
    public static final int LOGIN_SUCCESS = 200;
    public static final int LOGIN_IMEI_NULL = 201;
    public static final int LOGIN_USERNAME_NULL = 202;
    public static final int LOGIN_PASSWORD_NULL = 203;
    public static final int LOGIN_CLIENT_INVALID = 204;
    public static final int LOGIN_ACCOUNT_OR_PASSWORD_ERROR = 205;

    // 2.授权请求接口
    public static final int AUTHORIZE_REQUEST_SUCCESS = 300;
    public static final int AUTHORIZE_USERNAME_NULL = 301;
    public static final int AUTHORIZE_SN_NULL = 302;
    public static final int AUTHORIZE_USERNAME_OR_SN_ERROR = 303;
    public static final int AUTHORIZE_OVERDUE = 304;
    public static final int AUTHORIZE_NUM_SINGLE_EXCEEDED = 305;
    public static final int AUTHORIZE_NUM_TOTAL_EXCEEDED = 306;
    public static final int AUTHORIZE_SERIALNUMBER_ERROR = 307;

    // 3.授权校验接口
    public static final int AUTHORIZE_CHECK_SUCCESS = 500;
    public static final int AUTHORIZE_CHECK_USERNAME_NULL = 501;
    public static final int AUTHORIZE_CHECK_SN_NULL = 502;
    public static final int AUTHORIZED = 503;
    public static final int AUTHORIZE_FAIL = 504;
    public static final int AUTHORIZE_CHECK_USERNAME_OR_SN_ERROR = 505;

    public APIException(int resultCode) {
        this(getDetailMessage(resultCode));
    }

    public APIException(String detailMessage) {
        super(detailMessage);
    }

    /**
     * 由于服务器传递过来的错误信息直接给用户看的话，用户未必能够理解
     * 需要根据错误码对错误信息进行一个转换，在显示给用户
     * @param resultCode 结果码
     * @return 描述信息
     */
    private static String getDetailMessage(int resultCode) {
        String message;
        switch (resultCode) {

            //
            case LOGIN_SUCCESS:
                message = "登录成功";
                break;

            case LOGIN_IMEI_NULL:
                message = "imei为空";
                break;

            case LOGIN_USERNAME_NULL:
                message = "用户名为空";
                break;

            case LOGIN_PASSWORD_NULL:
                message = "密码为空";
                break;

            case LOGIN_CLIENT_INVALID:
                message = "客户端无效";
                break;

            case LOGIN_ACCOUNT_OR_PASSWORD_ERROR:
                message = "账号或密码错误";
                break;

            //
            case AUTHORIZE_REQUEST_SUCCESS:
                message = "授权申请成功";

            case AUTHORIZE_USERNAME_NULL:
                message = "用户名为空";
                break;

            case AUTHORIZE_SN_NULL:
                message = "sn码为空";
                break;

            case AUTHORIZE_USERNAME_OR_SN_ERROR:
                message = "用户名或sn码错误";
                break;

            case AUTHORIZE_OVERDUE:
                message = "授权时间过期";
                break;

            case AUTHORIZE_NUM_SINGLE_EXCEEDED:
                message = "单次授权次数超额";
                break;

            case AUTHORIZE_NUM_TOTAL_EXCEEDED:
                message = "总授权次数超额";
                break;

            case AUTHORIZE_SERIALNUMBER_ERROR:
                message = "流水号错误";
                break;

            // 授权校验
            case AUTHORIZE_CHECK_SUCCESS:
                message = "授权成功";
                break;

            case AUTHORIZE_CHECK_USERNAME_NULL:
                message = "用户名为空";
                break;

            case AUTHORIZE_CHECK_SN_NULL:
                message = "sn码为空";
                break;

            case AUTHORIZE_CHECK_USERNAME_OR_SN_ERROR:
                message = "用户名或sn码错误";
                break;

            case AUTHORIZE_FAIL:
                message = "授权失败";
                break;

            case AUTHORIZED:
                message = "设备已授权";
                break;

            default:
                message = "未知错误";

        }
        return message;
    }

    public static void checkParam(HashMap<String, String> params) {
        if (params == null) {
            throw new APIException("参数为空");
        }

        // client_id
        if (params.containsKey(LoginContract.USER_NAME)) {
            String username = params.get(LoginContract.USER_NAME);
            if (TextUtils.isEmpty(username)) {
                throw new APIException("请输入用户名");
            } else {
                params.put(LoginContract.USER_NAME, Base64Helper.encodeToString(username));
            }
        }

        // password
        if (params.containsKey(LoginContract.PASSWORD)) {
            String password = params.get(LoginContract.PASSWORD);
            if (TextUtils.isEmpty(password)) {
                throw new APIException("请输入密码");
            } else {
                params.put(LoginContract.PASSWORD, MD5Helper.md5Decode32(password));
            }
        }

        // 参数为空
        for (String key : params.keySet()) {
            if (TextUtils.isEmpty(params.get(key))) {
                throw new APIException(key + " is null.");
            }
        }
    }
}